package edu.infomatik.tundra.pages;

import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;

import edu.infomatik.tundra.data.DAOtundra;
import edu.infomatik.tundra.data.IBean;
import edu.infomatik.tundra.logic.SprintBean;
import edu.infomatik.tundra.logic.USTaskBean;
import edu.infomatik.tundra.logic.UStoryBean;
import edu.infomatik.tundra.logic.WorkBean;
import edu.infomatik.tundra.logic.WorkKinds;
import edu.infomatik.tundra.logic.security.SystemAccessBean;

@SuppressWarnings("unused")
public class UnfinishedList {
	@SessionState
	private SystemAccessBean user;
	private boolean userExists;
	private List<WorkBean> wbList = new ArrayList<WorkBean>();
	@SessionState
	@Property
	private WorkBean work;
	@SessionState
	@Property
	private SprintBean sprint;
	@SessionState
	@Property
	private UStoryBean story;
	@SessionState
	@Property
	private USTaskBean task;
	@Inject
	private Messages message;
	@SessionState
	private Boolean newWork=new Boolean(false);
	
	String onActivate(){
		work=null;
		if (!userExists) return "Index";
		else return null;
	}
	
	public String getKindLabelText(){
		String result="";
		if (! wbList.isEmpty())
			for (WorkBean wb : wbList)
				if (work.getId()==wb.getId())
					switch (wb.getKind()){
					case WorkKinds.PROJECT_VALUE:
						result="<strong>"+message.get("WorkKinds.PROJECT")+"</strong>";
						break;
					case WorkKinds.SPRINT_VALUE:
						result="<strong>"+message.get("WorkKinds.SPRINT")+"</strong>";
						break;
					case WorkKinds.TASK_VALUE:
						result="<strong>"+message.get("WorkKinds.TASK")+"</strong>";
						break;
					case WorkKinds.USTORY_VALUE:
						result="<strong>"+message.get("WorkKinds.USTORY")+"</strong>";
						break;
					case WorkKinds.OTHER_VALUE:
						result="<strong>"+message.get("WorkKinds.OTHER")+"</strong>";
						break;
					}
		return result;
	}
	
	public List<WorkBean> getAllUnfinishedWork(){
		if (wbList.size()==0){
			for (IBean wb: DAOtundra.getInstance().getAllBeans(new WorkBean())){
				if ( ((WorkBean)wb).getKind()==WorkKinds.SPRINT_VALUE )
					if (((WorkBean) wb).getProjectId()>0 || !((WorkBean)wb).isFullEntered())
						wbList.add((WorkBean)wb);
			}
			for (IBean wb: DAOtundra.getInstance().getAllBeans(new UStoryBean())){
				if (((UStoryBean)wb).getKind()==WorkKinds.USTORY_VALUE)
					if( ( ((UStoryBean) wb).getProjectId()>0 && ((UStoryBean) wb).getSprintId()>0) || !((UStoryBean)wb).isFullEntered() )
						wbList.add((UStoryBean)wb);
			}
			for (IBean wb: DAOtundra.getInstance().getAllBeans(new USTaskBean())){
				if (((USTaskBean) wb).getKind()==WorkKinds.TASK_VALUE)
					if( ((USTaskBean) wb).getStoryId()>0 || !((USTaskBean)wb).isFullEntered()  )
						wbList.add((USTaskBean)wb);
			}
		}
		return wbList;
	}
	@OnEvent(component="unfinishedWorkNameClick")
	String onNameClicked(long id){
		String result=onEditClicked(id);
//		if (wbList.size()>0){
//			for(IBean wb: wbList){
//				if (wb.getId()==id) work=(WorkBean)wb;
//				if (((WorkBean)wb).getKind() ==  WorkKinds.SPRINT_VALUE){
//					sprint=(SprintBean)wb;
//					result="TheSprint";
//				}else if (((WorkBean)wb).getKind() ==  WorkKinds.USTORY_VALUE){
//					story=(UStoryBean)wb;
//					result="TheStory";
//				}else if (((WorkBean)wb).getKind() ==  WorkKinds.TASK_VALUE){
//					task=(USTaskBean)wb;
//					result="";
//				}
//				else result="TheProject";
//			}
//		}
		return result;
	}

	@OnEvent(component="deleteWork")
	String onDeleteClicked(long id){
		if (wbList.size()>0){
			int i=0;
			for(WorkBean wb: wbList){
				if (wb.getId()==id) {
					wbList.remove(i);
					return "UnfinishedList";
				}else i++;
			}
		}
		return null;
	}
	@OnEvent(component="editWork")
	String onEditClicked(long id){
		String openPage="UnfinishedList";
		if (wbList.size()>0){
			for(IBean bean: wbList)
				if (bean.getId()==id) work=(WorkBean)bean;
		}else work=null;
		sprint=null;
		story = null;
		task=null;
		if ( WorkKinds.PROJECT_VALUE == work.getKind())	
			openPage="CreateProject";
		else if(WorkKinds.SPRINT_VALUE == work.getKind()){
			sprint = (SprintBean)work;
			openPage="CreateSprint";
		}else if(WorkKinds.USTORY_VALUE == work.getKind()){
			story = (UStoryBean)work;
			openPage="CreateUStory";
		}else if(WorkKinds.TASK_VALUE == work.getKind()){
			task = (USTaskBean)work;
			openPage="CreateTask";
		}

		return openPage;
	}
}
